﻿using _10_HPBar.GameFrame;
using _10_HPBar.GameFrame.System;
using _10_HPBar.GameFrame.Interface;
using _10_HPBar.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_HPBar.CharacterBase
{
    enum Direction
    {
        Up, Down, Left, Right
    }

    internal class Character : GameObject, IAttack
    {
        // 如果把成员变量公开 public
        // 不安全 因为外界能随意改动 破坏了类的封装性
        // 希望外界能获取 但不能修改
        public string? Name { get; }
        public static bool IsDialoging { get; set; }
        public static bool IsCombating { get; set; }
        public int Hp { get; set; }
        public int Mp { get; set; }
        public int Atk { get; set; }
        public int Def { get; set; }
        public bool IsDead { get; set; }

        protected Map map;
        protected string display;
        protected InfoSystem info;
        Random rand;

        // 属性与访问器
        // 访问器可以单独设置访问权限
        // 本身的访问权限范围 要大于 访问器的访问权限范围
        // 构造函数可以无视访问权限
        // get set 格式需要对应 要不就都; 要不就都{}
        // set 方法中 value关键字就指代外界传入的值 (=的右值)

        //public int GetIsDead()
        //{
        //    return IsDead ? 1 : 0;
        //}


        protected Character(string name, int hp, int mp, int atk, int def, Pos pos, Map map, string display, InfoSystem info)
        {
            Name = name;
            Hp = hp;
            Mp = mp;
            Atk = atk;
            Def = def;
            Pos = pos;
            this.map = map;
            this.display = display;
            IsDead = false;
            IsDialoging = false;
            IsCombating = false;
            this.info = info;
            rand = new Random();
        }

        public virtual void Init()
        {
        }

        public virtual void Render()
        {
            Utility.MyPrint(display, Pos.x + map.offsetX, Pos.y + map.offsetY);
        }

        public void Attack(IAttack target)
        {
            if (Atk > target.Def)
            {
                info.AddInfo($"{Name}攻击了{((Character)target).Name}!", ConsoleColor.Green);
                target.TakeDamage(Atk - target.Def);
            }
            else
            {
                info.AddInfo($"{Name}攻击了{((Character)target).Name},攻击无效!", ConsoleColor.DarkGray);
            }
        }

        public void TakeDamage(int damage)
        {
            info.AddInfo($"{Name}受到了{damage}点伤害!", ConsoleColor.Yellow);
            Hp -= damage;
            if (Hp < 0) Hp = 0;
            // 死亡判定
            IsDead = Hp == 0;
            if (IsDead)
            {
                info.AddInfo($"{Name}死亡!", ConsoleColor.Red);
                OnDead();
            }
        }

        public virtual void OnDead()
        {
            // 告诉战斗系统 战斗结束
            EventSystem.Instance.InvokeEvent("战斗结束");
        }

        public void Skill(IAttack target)
        {
            info.AddInfo($"{Name}对{((Character)target).Name}使用了技能", ConsoleColor.Blue);
            target.TakeDamage(10);
        }

        public void Item(IAttack target)
        {
            info.AddInfo($"{Name}对{((Character)target).Name}使用了道具", ConsoleColor.Yellow);
            target.TakeDamage(5);
        }

        public void Escape()
        {
            int i = rand.Next(100);
            if (i > 70)
            {
                info.AddInfo($"{Name}逃跑成功", ConsoleColor.Green);
                // 告诉战斗系统 战斗结束
                EventSystem.Instance.InvokeEvent("战斗结束");
            }
            else
            {
                info.AddInfo($"{Name}逃跑失败", ConsoleColor.Red);
            }
        }
    }
}